# -*- coding: utf-8 -*-
# @Date：2024/8/6 16:11:06
# @Author：CJiaの青姝
# @FileName：operate_log
# @Editor：PyCharm2024
# @Remark：
from typing import List

from logs.models import OperationLog
from ninja import Router, Field, Query, ModelSchema
from ninja.pagination import paginate

from utils.cj_ninja import CjFilters, MyPagination
from utils.cj_crud import condition, single
from utils.cj_response import SuccessResponse

router = Router()


class Filters(CjFilters):
    request_username: str = Field(None, description="操作用户", alias='request_username')

    class Config:
        title = "操作日志查询参数"


class OperateLogSchemaOut(ModelSchema):
    class Config:
        title = "操作日志实例"
        model = OperationLog
        model_fields = "__all__"


@router.get("/operate_log", response=List[OperateLogSchemaOut], summary="获取分页操作日志列表")
@paginate(MyPagination)
def list_operate_log(request, filters: Filters = Query(...)):
    """ 获取分页操作日志列表 """
    # 调用分页方法
    result = condition(request, OperationLog, filters)
    # 返回结果
    return result


@router.get("/operate_log/{operate_log_id}", response=OperateLogSchemaOut, summary="获取操作日志")
def get_operate_log(request, operate_log_id: int):
    """ 获取操作日志 """
    # 调用根据id获取指定对象方法
    result = single(request, OperationLog, id=operate_log_id)
    # 返回结果
    return SuccessResponse(data=result)
